using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Omniture.API
{
	internal class XmlHelper
	{
		public static void AddAttribute(XmlNode node, string name, string value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value;
			node.Attributes.Append(attr);
		}

		public static void AddAttribute(XmlNode node, string name, bool value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value.ToString();
			node.Attributes.Append(attr);
		}

		public static void AddAttribute(XmlNode node, string name, int value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value.ToString();
			node.Attributes.Append(attr);
		}

		public static void AddAttribute(XmlNode node, string name, long value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value.ToString();
			node.Attributes.Append(attr);
		}

		public static void AddAttribute(XmlNode node, string name, Guid value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value.ToString();
			node.Attributes.Append(attr);
		}

		public static void AddAttribute(XmlNode node, string name, double value)
		{
			XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
			attr.Value = value.ToString();
			node.Attributes.Append(attr);
		}

		public static string GetAttribute(XmlNode node, string attributeName)
		{
			return GetAttribute(node, attributeName, "");
		}

		public static string GetAttribute(XmlNode node, string attributeName, string defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return attr.Value;
		}

		public static Guid GetAttribute(XmlNode node, string attributeName, Guid defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return new Guid(attr.Value);
		}

		public static double GetAttribute(XmlNode node, string attributeName, double defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return double.Parse(attr.Value);
		}

		public static int GetAttribute(XmlNode node, string attributeName, int defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return int.Parse(attr.Value);
		}

		public static long GetAttribute(XmlNode node, string attributeName, long defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return long.Parse(attr.Value);
		}

		public static bool GetAttribute(XmlNode node, string attributeName, bool defaultValue)
		{
			XmlAttribute attr = node.Attributes[attributeName];
			if (attr == null)
				attr = node.Attributes[attributeName.ToLower()];

			if (attr == null)
				return defaultValue;
			else
				return bool.Parse(attr.Value);
		}

		public static XmlNodeList SelectNodes(XmlNode node, string xpath)
		{
			XmlNodeList retval1 = node.SelectNodes(xpath);
			XmlNodeList retval2 = node.SelectNodes(xpath.ToLower());
			if (retval1.Count > 0)
				return retval1;
			else
				return retval2;
		}

		public static XmlElement AddNode(XmlNode parent, string name)
		{
			return AddNode(parent, name, null);
		}

		public static XmlElement AddNode(XmlNode parent, string name, object nodeValue)
		{
			try
			{
				XmlElement retval = null;
				if (nodeValue == null)
				{
					retval = (XmlElement)parent.OwnerDocument.CreateNode(XmlNodeType.Element, name, "");
				}
				else
				{
					retval = parent.OwnerDocument.CreateElement(name);
					retval.InnerText = nodeValue.ToString();
				}

				parent.AppendChild(retval);
				return retval;

			}
			catch (Exception ex)
			{
				throw;
			}
		}

		public static string GetChildValue(XmlNode parentNode, string childName)
		{
			return GetChildValue(parentNode, childName, null);
		}

		public static double GetChildValue(XmlNode parentNode, string childName, double defaultValue)
		{
			XmlNode node = parentNode.SelectSingleNode(childName);
			if (node != null)
				return double.Parse(node.InnerText);
			else
				return defaultValue;
		}

		public static int GetChildValue(XmlNode parentNode, string childName, int defaultValue)
		{
			XmlNode node = parentNode.SelectSingleNode(childName);
			if (node != null)
				return int.Parse(node.InnerText);
			else
				return defaultValue;
		}

		public static bool GetChildValue(XmlNode parentNode, string childName, bool defaultValue)
		{
			XmlNode node = parentNode.SelectSingleNode(childName);
			if (node != null)
				return bool.Parse(node.InnerText);
			else
				return defaultValue;
		}

		public static Guid GetChildValue(XmlNode parentNode, string childName, Guid defaultValue)
		{
			XmlNode node = parentNode.SelectSingleNode(childName);
			if (node != null)
				return new Guid(node.InnerText);
			else
				return defaultValue;
		}

		public static string GetChildValue(XmlNode parentNode, string childName, string defaultValue)
		{
			XmlNode node = parentNode.SelectSingleNode(childName);
			if (node != null)
				return node.InnerText;
			else
				return defaultValue;
		}

	}
}
